package fei.zhou.elasticsearch01;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

public class ESTools {
    
    public static String ipStr1 = "60.205.188.229";
    public static String ipStr2 = "60.205.188.229";
    public static String ipStr3 = "60.205.188.229";
    public static int port1 = 9301;
    public static int port2 = 9302;
    public static int port3 = 9303;
    // 集群名称
    public static String clusterName = "cluster.name";
    // 集群名称的值
    public static String clusterNameValue = "elasticsearch";
    
    /**
     * 创建客户端
     * 
     * @author feizhou
     * @since 2020/12/28 11:10
     */
    public static TransportClient createClient() throws UnknownHostException {
        
        // 创建一个Settings对象
        Settings settings = Settings.builder()
                // 构建集群名称
                .put(clusterName, clusterNameValue).build();
        // 创建一个TransPortClient对象
        TransportClient client = new PreBuiltTransportClient(settings);
        // 添加集群间的通讯
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ipStr1), port1));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ipStr2), port2));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ipStr3), port3));
        
        System.out.println("创建客户端成功");
        return client;
    }
    
    /**
     * 关闭客户端
     * 
     * @author feizhou
     * @since 2020/12/28 11:10
     */
    public static void closeClient(TransportClient client) {
        client.close();
        System.out.println("成功关闭客户端");
    }
    
    /**
     * 查询
     * 
     * @author feizhou
     * @since 2020/12/28 12:00
     */
    public static SearchHit[] search(QueryBuilder queryBuilder, String indices, String types) throws Exception {
        
        TransportClient client = createClient();
        
        // 执行查询
        SearchResponse searchResponse = client.prepareSearch(indices).setTypes(types).setQuery(queryBuilder)
                // 执行查询
                .get();
        // 取查询结果
        SearchHits searchHits = searchResponse.getHits();
        
        SearchHit[] hits = searchHits.getHits();
        
        // 关闭client
        closeClient(client);
        return hits;
        
    }

    /**
     * 分页查询
     * 
     * @author feizhou
     * @since 2020/12/28 12:00
     */
    public static SearchHits searchPage(QueryBuilder queryBuilder, String indices, String types, int startIndex,
            int pageSize) throws Exception {
        
        TransportClient client = createClient();
        
        // 执行查询
        SearchResponse searchResponse = client.prepareSearch(indices).setTypes(types).setQuery(queryBuilder)
                // 设置分页信息
                .setFrom(startIndex)
                // 每页显示的行数
                .setSize(pageSize)
                // 执行查询
                .get();
        // 取查询结果
        SearchHits searchHits = searchResponse.getHits();
        // 关闭client
        closeClient(client);
        return searchHits;
        
    }

}
